<?PHP
	session_start();
	$conn = mysql_connect("", "piedu_user", "q&&^%@8ezO8+");
	mysql_select_db("piedu_db");
	
	if(!array_key_exists("cart", $_SESSION)){
		$_SESSION['cart'] = array();
	}
	if(array_key_exists("item", $_GET)){
		$exists = false;
		foreach($_SESSION['cart'] as $exist){
			if($exist['id'] == $_GET['item']){
				$exists = true;
			}
		}
		if($exists == false){
			$sql = "SELECT * from items WHERE id = {$_GET['item']}";
			$query = mysql_query($sql, $conn);
			$item = mysql_fetch_assoc($query);
			
			$array_counter = count($_SESSION['cart']);
			$_SESSION['cart'][$array_counter]['id'] = $item['id'];
			$_SESSION['cart'][$array_counter]['sku'] = $item['sku'];
			$_SESSION['cart'][$array_counter]['qty'] = 1;
			$_SESSION['cart'][$array_counter]['description'] = $item['description'];
			$_SESSION['cart'][$array_counter]['price'] = $item['price'];
			$_SESSION['cart'][$array_counter]['base_price'] = $item['price'];
		}
	} else if(array_key_exists("update", $_POST)){ //Item update quantity
		foreach($_SESSION['cart'] as &$items){
			if(array_key_exists($items['sku'], $_POST)){
				$items['qty'] = $_POST[$items['sku']];
			}
		}
	} else if(array_key_exists("delete", $_GET) && array_key_exists("id", $_GET)){
		foreach($_SESSION['cart'] as $key => $cart_item){
			if($cart_item['id'] == $_GET['id']){
				$to_remove = $key;
			}
		}
		unset($_SESSION['cart'][$to_remove]);
	}
	
	//Get the promotions
	$promotions = array();
	$sql = "SELECT * FROM promotions WHERE start_date <= now() AND end_date >= now()";
	$promotions_query = mysql_query($sql, $conn);
	if(mysql_num_rows($promotions_query) > 0){
		while($row = mysql_fetch_assoc($promotions_query)){
			$promotions[] = $row;
		}
	} else {
		$promotions = null;
	}
	
	foreach($_SESSION['cart'] as &$row){
	$row['price'] = $row['base_price'];
	if($promotions != null){
			foreach($promotions as $promo){
				$array_items = explode(",", $promo['items']);
				if(count($array_items) > 0){
					foreach($array_items as $value){
						if($value == $row['sku']){
							if($promo['discount_type'] == "USD"){
								$row['price'] -= $promo['discount_amount'];
							} else {
								$row['price'] -= ($promo['discount_amount']/100)*$row['price'];
							}
						}
					} //End foreach
				}
				if($promo['promotion_type'] == "general"){
					if($promo['discount_type'] == "USD"){
						$row['price'] -= $promo['discount_amount'];
					} else {
						$row['price'] -= ($promo['discount_amount']/100)*$row['price'];
					}
				} else if($promo['promotion_type'] == $row['category']){
					if($promo['discount_type'] == "USD"){
						$row['price'] -= $promo['discount_amount'];
					} else {
						$row['price'] -= ($promo['discount_amount']/100)*$row['price'];
					}
				}
			} //End foreach
		}
	}
	unset($row);
	//Recalculate the cart
	$_SESSION["Payment_Amount"] = 0.0;
	$_SESSION["payment_subtotal"] = 0.0;
	$_SESSION["total_discount"] = 0.0;
	
	if(count($_SESSION['cart']) > 0){
		foreach($_SESSION['cart'] as $v){
			$item_total_payment = $v['qty'] * $v['base_price'];
			$_SESSION["payment_subtotal"] += $item_total_payment;
			$_SESSION["total_discount"] += ($v['base_price'] - $v['price']) * $v['qty'];
		}
	}
	$_SESSION["Payment_Amount"] = $_SESSION["payment_subtotal"] - $_SESSION["total_discount"];
	echo $_SESSION["Payment_Amount"];

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Pie Education | My Cart</title>
<Meta name="Keywords" content="education, course, learning, online, virtual, e-learning, school, student, teacher, homework, educational platform, educational system, free tool">
<Meta name="Description" content="Pie Education is a internet based Course Management System (CMS)">
<Meta name="Author" content="Rob Keller">
<Meta name="Publisher" content="Aaron Aceves">
<Meta name="Copyright" content="Copyright© 2012, Pie Education">
<Meta name="Revisit-After" content="30 days">
<Meta HTTP-EQUIV="Expires" content="none">
<Meta name="Pagetopic" content="Education">
<Meta name="Audience" content=" All">
<Meta name="Robots" content="INDEX,FOLLOW">
<Meta name="Content-Language" content="English">
<link rel="shortcut icon" type="image/x-icon" href="/favicon.ico" />
<link rel="stylesheet" href="css/style.css" />
</head>

<body>
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td height="90" bgcolor="#FF8C00" align="center"><table width="980" border="0" align="center" cellpadding="0" cellspacing="0">
      <tr>
        <td><a href="index.php"><img src="images/logo.png" alt="Return to Index" width="235" height="63" border="0" title="pie.edu an E-Learning Network" /></a></td>
      </tr>
    </table></td>
  </tr>
  <tr>
    <td height="500" bgcolor="#FFFFFF"><table width="1100" border="0" align="center" cellpadding="4" cellspacing="4">
      <tr>
        <td width="250"><strong>Here goes the store menu</strong></td>
        <td><h1 style="color:#00F">My Cart Contents</h1></td>
      </tr>
      <tr>
        <td>&nbsp;</td>
        <td>
        <form action="basket.php" method="post" name="form4" id="form4">
        <table width="100%" border="0" align="center" cellpadding="2" cellspacing="4">
          <tr>
            <td width="55%" align="center" bgcolor="#f4f4f4"><strong>My Item</strong></td>
            <td width="15%" align="center" bgcolor="#f4f4f4"><strong>Quantity</strong></td>
            <td width="15%" align="center" bgcolor="#f4f4f4"><strong>Price</strong></td>
            <td width="15%" align="center" bgcolor="#f4f4f4"><strong>Remove</strong></td>
          </tr>
          <?php
          if(count($_SESSION['cart']) > 0){
			  foreach($_SESSION['cart'] as $cart){
			  echo '<tr>
				<td><a href="product_description.php" style="font-size:14px; color:#F60"><strong>'.$cart['description'].'</strong></a><br />
				  <a href="product_description.php" style="font-size:10px">SKU - '.$cart['sku'].'</a></td>
				<td align="center"><input name="update" type="hidden" id="update" value="true" /><input name="'.$cart['sku'].'" type="text" id="'.$cart['sku'].'" value="'.$cart['qty'].'" size="5" style="text-align:center" /></td>
				<td align="center">$'.$cart['price'].'</td>
				<td align="center"><a href="basket.php?delete=true&amp;id='.$cart['id'].'">remove</a></td>
			  </tr>';
			  }
		  } else {
			  echo '<tr>
            <td align="center" style="color:#F00"><strong>Currently there are no items on your cart, <a href="store.php">keep shopping</a></strong></td>
          </tr>';
		  }
          ?>
        </table></form></td>
      </tr>
      <tr>
        <td>&nbsp;</td>
        <td><table width="100%" border="0" align="center" cellpadding="4" cellspacing="4">
          <tr>
            <td width="60%"><form id="form1" name="form1" method="post" action="basket.php">
              <table width="100%" border="0" align="center" cellpadding="4" cellspacing="4">
                <tr>
                  <td colspan="2" style="color:#999"><strong>If you have a coupon enter here and click submit</strong></td>
                </tr>
                <tr>
                  <td width="50%"><label for="coupon"></label>
                    <input name="coupon" type="text" class="coupon" id="coupon" /></td>
                  <td><input name="enviar" type="submit" class="submit" id="enviar" value="Submit" /></td>
                </tr>
              </table>
            </form></td>
            <td><table width="100%" border="0" cellspacing="4" cellpadding="2">
              <tr>
                <td align="right" style="color:#999"><strong>Order Subtotal</strong></td>
                <td width="30%" align="center" style="color:#999"><strong>$ <?php echo $_SESSION["payment_subtotal"] ?></strong></td>
              </tr>
              <tr style="color:#F00">
                <td align="right"><strong>Discounts</strong></td>
                <td align="center"><strong>$ <?php echo $_SESSION["total_discount"] ?></strong></td>
              </tr>
              <tr>
                <td align="right"><strong>Order Total</strong></td>
                <td align="center"><strong>$ <?php echo $_SESSION["Payment_Amount"]; ?></strong></td>
              </tr>
            </table></td>
          </tr>
        </table></td>
      </tr>
      <tr>
        <td>&nbsp;</td>
        <td><table width="100%" border="0" cellspacing="4" cellpadding="4">
          <tr>
            <td align="center">&nbsp;</td>
            <td width="50%" align="center"><input name="enviar2" type="button" class="submit" id="enviar2" value="Keep Shopping" onclick="location.href='store.php'" />
              <?php if(count($_SESSION['cart']) > 0){ echo '<input name="enviar3" type="submit" class="submit" id="enviar3" value="Recalculate" onclick="document.form4.submit()" />'; } ?></td>
            </tr>
          <tr>
            <td align="center">&nbsp;</td>
            <td align="center"><?php if(count($_SESSION['cart']) > 0){ echo "<form action='expresscheckout.php' METHOD='POST'>
              <input type='image' name='submit' src='https://www.paypal.com/en_US/i/btn/btn_xpressCheckout.gif' border='0' align='top' alt='Check out with PayPal'/>
              </form>"; } ?></td>
            </tr>
          </table></td>
      </tr>
    </table></td>
  </tr>
  <tr>
    <td height="80" bgcolor="#FF9933" align="center"><a href="index.php" class="bottom-general-menu" style="color:#FFF">Home</a> <a href="platform.php" class="bottom-general-menu" style="color:#FFF">Platform</a> <a href="piephone.php" class="bottom-general-menu" style="color:#FFF">Pie Phone</a> <a href="company_training.php" class="bottom-general-menu" style="color:#FFF">Company Training</a> <a href="jobs.php" class="bottom-general-menu" style="color:#FFF">Jobs</a> <a href="press.php" class="bottom-general-menu" style="color:#FFF">Press</a> <a href="community/" class="bottom-general-menu" style="color:#FFF">Community</a> <a href="mobile.php" class="bottom-general-menu" style="color:#FFF">Mobile</a> <a href="support/" class="bottom-general-menu" style="color:#FFF">Help</a> <a href="blog/" class="bottom-general-menu" style="color:#FFF">Blogs</a> <a href="forums/" class="bottom-general-menu" style="color:#FFF">Forums</a><a href="about.php" class="bottom-general-menu" style="color:#FFF">About</a> <a href="terms.html" class="bottom-general-menu" style="color:#FFF">Terms of Use</a> <a href="privacy.html" class="bottom-general-menu" style="color:#FFF">Privacy Policy</a> <a href="contact-us.php" class="bottom-general-menu" style="color:#FFF">Contact Us</a></td>
  </tr>
</table>
</body>
</html>
